package com.alibaba.citrus.cr.unified.inventory.functions.invoiceOrder;

import com.alibaba.citrus.cr.unified.inventory.utils.InfoUtil;
import com.epoch.app.crunifiedinventory.dto.request.InvoiceOrderCreateRequest;
import com.epoch.app.crunifiedinventory.dto.request.InvoiceOrderDetailCreateDTO;
import com.epoch.app.crunifiedinventory.sdo.InvoiceOrderDetailSDO;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

@Data
public class InvoiceOrderDetailEntity {
    private InvoiceOrderDetailSDO invoiceOrderDetailSDO;

    public void createInvoiceOrderDetailSDO(InvoiceOrderCreateRequest invoiceOrderCreateRequest, InvoiceOrderDetailCreateDTO request) {
        invoiceOrderDetailSDO = convertRequest2InvoiceOrderSDO(invoiceOrderCreateRequest, request);
    }

    private InvoiceOrderDetailSDO convertRequest2InvoiceOrderSDO(InvoiceOrderCreateRequest invoiceOrderCreateRequest, InvoiceOrderDetailCreateDTO request) {
        InvoiceOrderDetailSDO invoiceOrderDetailSDO = new InvoiceOrderDetailSDO();

        invoiceOrderDetailSDO.setInvoiceOrderId(invoiceOrderCreateRequest.getInvoiceOrderId());
        invoiceOrderDetailSDO.setOrderLineId(request.getOrderLineId());
        invoiceOrderDetailSDO.setScItemId(InfoUtil.deleteZeroPrefix(request.getScItemId()));
        invoiceOrderDetailSDO.setTitle(request.getTitle());
        if (StringUtils.isNotBlank(request.getQuantity())) {
            BigDecimal quantity = new BigDecimal(request.getQuantity().trim());
            invoiceOrderDetailSDO.setQuantity(quantity.stripTrailingZeros().toPlainString());
        }
        invoiceOrderDetailSDO.setCompanyCode(invoiceOrderCreateRequest.getCompanyCode());
        invoiceOrderDetailSDO.setPrice(request.getPrice());
        invoiceOrderDetailSDO.setSubTotalAmount(request.getSubTotalAmount());
        invoiceOrderDetailSDO.setDeliveryOrderId(request.getDeliveryOrderId());
        invoiceOrderDetailSDO.setSourceOrderId(request.getSourceOrderId());
        invoiceOrderDetailSDO.setFeature(request.getFeature());
        invoiceOrderDetailSDO.setSaleOrganizationCode(invoiceOrderCreateRequest.getSaleOrganizationCode());
        // 源单创建时间
        invoiceOrderDetailSDO.setSourceOrderCreateTime(invoiceOrderCreateRequest.getSourceOrderCreateTime());
        // 交货单行号
        invoiceOrderDetailSDO.setDeliveryOrderLineId(InfoUtil.deleteZeroPrefix(request.getDeliveryOrderLineId()));
        // 销售订单行号
        invoiceOrderDetailSDO.setSalesOrderLineId(InfoUtil.deleteZeroPrefix(request.getSalesOrderLineId()));
        invoiceOrderDetailSDO.setSapGenerateTime(invoiceOrderCreateRequest.getSapGenerateTime());
        return invoiceOrderDetailSDO;
    }
}
